<template>
  <div style="margin-top: 20px;">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="姓名：">
        <el-input v-model="formInline.doctorName" placeholder="请输入姓名" clearable />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="getTellList()">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="序号" align="center" width="55" />
      <el-table-column prop="bedCode" label="床位" align="center" />
      <el-table-column prop="name" label="病人" align="center" />
      <el-table-column prop="doctorContent" label="医嘱内容" align="center">
        <template #default="scope">
          <span :style="{ color: scope.row.isPublisher == 0 ? '#F56C6C' : '#409EFF' }">
            {{ scope.row.doctorContent }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="doctorName" label="医生姓名" align="center" />
      <el-table-column prop="doctorSTime" label="医嘱开始日期" align="center">
        <template #default="scope">
          {{ scope.row.doctorSTime.substring(0, 10).replace('T', ' ') }}
        </template>
      </el-table-column>
      <el-table-column prop="doctorETime" label="医嘱结束日期" align="center">
        <template #default="scope">
          {{ scope.row.doctorETime.substring(0, 10).replace('T', ' ') }}
        </template>
      </el-table-column>
      <el-table-column prop="isPublisher" label="状态" align="center">
        <template #default="scope">
          <el-tag :type="isEffective(scope.row.doctorSTime, scope.row.doctorETime) ? 'success' : 'danger'" effect="dark">
            {{ isEffective(scope.row.doctorSTime, scope.row.doctorETime) ? '生效中' : '失效' }}
          </el-tag>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, onUnmounted, computed } from 'vue'
import http from '@/api/http.js'
import * as signalR from '@microsoft/signalr'
import { ElMessage } from 'element-plus'

// 判断医嘱是否生效的方法
const isEffective = (startDate, endDate) => {
  const now = new Date()
  const start = new Date(startDate)
  const end = new Date(endDate)
  
  // 如果当前日期在开始日期和结束日期之间（含边界），则生效中
  return now >= start && now <= end
}

// SignalR连接
let connection = null;

// 初始化SignalR连接
const initSignalRConnection = () => {
  if (!connection) {
    connection = new signalR.HubConnectionBuilder()
      .withUrl('http://localhost:9991/sendClub')
      .withAutomaticReconnect()
      .build();
      
    // 启动连接
    connection.start()
      .then(() => {
        console.log('Tell.vue: SignalR连接成功建立');
      })
      .catch(err => {
        console.error('Tell.vue: SignalR连接失败:', err);
      });
    
    // 监听医嘱更新消息
    connection.on('GetTell', (message) => {
      console.log('收到医嘱更新消息:', message);
      ElMessage.info('收到新的医嘱更新通知');
      // 刷新数据
      getTellList();
    });
  }
}

//查询列表数据
const formInline = reactive({
  doctorName: ''
})

// 获取列表数据
const tableData = ref([])
const getTellList = () => {
  http
    .get(`api/TellController/GetTell?name=${formInline.doctorName}`)
    .then((res) => {
      tableData.value = res
    })
}

onMounted(() => {
  getTellList()
  initSignalRConnection()
})

// 组件卸载时关闭连接
onUnmounted(() => {
  if (connection) {
    connection.stop()
    connection = null
  }
})
</script>

<style scoped>
.operation-wrapper {
  display: flex;
  justify-content: flex-start;
  margin-bottom: 20px;
}
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style>